package com.zhangwei.day41photo.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.zhangwei.xutils.utils
 * @Description:
 * @date 15/10/17 上午10:46
 */

public class FileUtils {
    public static File getRootCache(Context context) {
        File rootFile = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            rootFile = context.getExternalCacheDir();
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                rootFile = new File(Environment.getExternalStorageDirectory() + File.separator + AppConfig.ROOT_CACHE);
            } else {
                rootFile = new File(Environment.getDataDirectory() + File.separator + context.getPackageName());
            }
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
        }
        return rootFile;
    }

    //sdk/qianfeng/imageCache
    public static String getSaveBitmapPath(Context context) {
        String imagePath = getRootCache(context) + File.separator + AppConfig.IMAGE_CACHE;
        if (!new File(imagePath).exists()) {
            boolean mkdirs = new File(imagePath).mkdirs();
        }
        return imagePath;
    }

    /**
     * 获取拍照文件夹的路径
     *
     * @return
     */
    public static File getPhotoCache(Context context) {
        String photoPath = getRootCache(context) + File.separator + AppConfig.PHOTO_CACHE;
        File photoFile = new File(photoPath);
        boolean mkdirs;
        if (!photoFile.exists()) {
            mkdirs = photoFile.mkdirs();
        }
        return photoFile;
    }

    /**
     * 获取保存图片的文件
     *
     * @return
     */
    public static File getPhotoFile(Context context) {
        //生成文件名称+文件格式
        String photoPath = getPhotoCache(context) + File.separator + "IMG_" + new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date()) + ".jpg";
        File photoFile = new File(photoPath);
        if (!photoFile.exists()) {
            try {
                photoFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return photoFile;
    }

    public static File getCompressFile(Context context, File orgFile) {
        String tempPath = getPhotoCache(context) + File.separator + "temp";
        File temPath = new File(tempPath);
        if (!temPath.exists()) {
            temPath.mkdirs();
        }

        File tempImageFile = new File(tempPath + File.separator + orgFile.getName());

        if (!tempImageFile.exists()) {
            try {
                tempImageFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    return tempImageFile;
    }


    /**
     * 获取数据库路径
     *
     * @param context
     * @return
     */
    public static String getDBPath(Context context) {
        String dbPath = getRootCache(context) + File.separator + AppConfig.DB_CACHE;
        if (!new File(dbPath).exists()) {
            boolean mkdirs = new File(dbPath).mkdirs();
        }

        return dbPath;
    }


    public static File getDbFile(Context context) {
        String dbPath = getRootCache(context) + File.separator + AppConfig.DB_DIR;
        File dbFile = new File(dbPath);
        if (!dbFile.exists()) {
            dbFile.mkdir();
        }
        return dbFile;
    }

}
